package com.tbit.uqbike.webmanager.controller;

import com.tbit.uqbike.object.pojo.UserAuthLog;
import com.tbit.uqbike.object.pojo.dto.UserAuthLogDTO;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.common.entity.view.PageResult;
import com.tbit.common.entity.view.Result;
import com.tbit.uqbike.service.business.UserAuthLogService;
import com.tbit.uqbike.webmanager.util.excel.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 用户特殊认证记录
 *
 * @Author:BUGTian
 * @DATE: 2022/9/23
 */
@RestController
@RequestMapping("/userAuthLog")
public class UserAuthLogController {

    @Autowired
    private UserAuthLogService userAuthLogService;


    /**
     * 分页查询
     *
     * @param page        当前页码
     * @param size        每页显示的行
     * @param accountId   品牌id
     * @param adAccountId 区域id
     * @param userName    用户名
     * @param phone       手机号
     * @param configId    认证类型
     * @param state       审核状态 0未处理、1审核成功、2审核失败
     * @return
     */
    @GetMapping("/queryPage")
    public Result<PageResult<UserAuthLog>> queryPage(Integer page, Integer size, Integer accountId, Integer adAccountId, String userName, String phone, Integer configId, Integer state, Date startTime, Date endTime) {
        return userAuthLogService.queryPage(page, size, accountId, adAccountId, userName, phone, configId, state, startTime, endTime);
    }

    /**
     * 用户特殊认证记录导出
     * @return
     */
    @GetMapping("/exportUserAuth")
    public Result<Object> exportUserAuth(Integer accountId, Integer adAccountId, String userName, String phone, Integer configId, Integer state, Date startTime, Date endTime) {
        Result<PageResult<UserAuthLog>> pageResultResult = userAuthLogService.queryPage(1, 65535, accountId, adAccountId, userName, phone, configId, state, startTime, endTime);
        List<UserAuthLog> list = pageResultResult.getData().getList();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String fileName = String.format("%s~%s 用户特殊认证记录", sdf.format(startTime), sdf.format(endTime));
        ExcelUtil<UserAuthLog> excelUtil = new ExcelUtil<>(UserAuthLog.class);
        JsonResult result = excelUtil.exportExcel(list, fileName);
        return Result.success(result.getData());
    }

    /**
     * 审核
     *
     * @param request
     * @param userAuthLogDTO 审核信息
     * @return
     */
    @PostMapping("/audit")
    public Result audit(HttpServletRequest request, @RequestBody UserAuthLogDTO userAuthLogDTO) {
        LoginInfo login = BaseController.getLogin(request);
        return userAuthLogService.audit(login, userAuthLogDTO);
    }

    /**
     * 清除用户特殊认证记录
     *
     * @param userId
     * @return
     */
    @DeleteMapping("/cleanUserAuth")
    public Result cleanUserAuth(Integer userId) {
        return userAuthLogService.cleanUserAuth(userId);
    }
}
